package com.lanou.activity.handle;

import com.lanou.activity.exception.PlainException;
import com.lanou.activity.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author lanouhn 书豪
 * @description 统一异常处理
 * @create 17/9/25
 */
@ControllerAdvice
public class ExceptionHandle {

    private Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Map<String,Object> exceptionHandle(Exception e){
        if (e instanceof PlainException){
            PlainException plainException = (PlainException) e;
            logger.error("【自定义异常plain】{}{}",e.toString(),plainException.getMessage());
            return ResultUtil.error(plainException.getCode(),plainException.getMsg());
        }else {
            logger.error("【系统异常】{}{}",e.toString(), e.getStackTrace());
            return ResultUtil.error(444,"连接失败,系统异常");
        }
    }
}
